import { Injectable } from '@angular/core'
import { TreeModel } from 'ng2-tree';
import { Http, RequestOptionsArgs } from '@angular/http';

import { GeoJsonLayer } from '../data_model/data-model'

import VectorLayer from 'ol/layer/vector'
import Vector from 'ol/source/vector'
import VectorTile from 'ol/source/vectortile'
import GeoJSON from 'ol/format/GeoJSON'
import Proj from 'ol/proj'
import BingMaps from 'ol/source/bingmaps'
import OlMap from 'ol/map'
import View from 'ol/view'
import TileLayer from 'ol/layer/tile'
import XYZ from 'ol/source/xyz'
import OSM from 'ol/source/osm'
import Extent from 'ol/extent'

@Injectable()
export class OlMapService {

    private LayersMap: Map<string, any>;
    constructor(private http: Http) {
        this.LayersMap = new Map<string, any>();
    }
    getGeoJsonByFile(file: File): Promise<any> {
        return new Promise((resolve, reject) => {
            if (!file) {
                reject('Input file in null');
            } else {
                let postData = new FormData();
                postData.append('shpfile', file);
                this.http.post("/api/shp2json", postData)
                    .toPromise().then(data => {
                        resolve(data);
                    }, error => {
                        reject(error);
                    })
            }
        })
    }


    //将geojson添加至Vector图层
    addVectorLayer(MapObject: OlMap, geoJsonLayer: GeoJsonLayer) {
        let geoJsonObject = JSON.parse(geoJsonLayer.data);

        let vectorSource = new Vector({
            features: (new GeoJSON()).readFeatures(geoJsonObject)
        });

        let vectorLayer = new VectorLayer({
            source: vectorSource
        })

        //判断是否有投影信息
        if (geoJsonObject.crs) {
            this.setProjection(MapObject, '4326', vectorSource.getExtent());
        } else {
            this.setProjection(MapObject, null, vectorSource.getExtent());
        }
        this.LayersMap.set(geoJsonLayer.id, vectorLayer);
        MapObject.addLayer(vectorLayer);

    }

    //将geojson添加至VectorTile图层
    addVectorTileLayer(MapObject: OlMap, geoJsonLayer: GeoJsonLayer) {
       /////有问题
    }

    showOrHideLayer(MapObject: OlMap, id: string) {
        let CurrentLayer = this.LayersMap.get(id);
        if (CurrentLayer) {
            if (CurrentLayer.getVisible()) {
                CurrentLayer.setVisible(false);
            } else {
                CurrentLayer.setVisible(true);
            }

        }
    }


    removeLayer(MapObject: OlMap, id: string) {
        let CurrentLayer = this.LayersMap.get(id);
        if (CurrentLayer) {
            MapObject.removeLayer(CurrentLayer);
            this.LayersMap.delete(id);
        
        }

    }
    //投影转换
    setProjection(MapObject: OlMap, code: string, FeatureExtent?: Array<number>): void {
        if (code === null) {
            MapObject.setView(new View({
                projection: 'EPSG:3857',
                center: [0, 0],
                zoom: 2,
                minZoom: 2
            }));
            return;
        }
        let newProjCode = 'EPSG:' + code;
        let newProj = Proj.get('EPSG:' + code);

        let fromLonLat = Proj.getTransform('EPSG:4326', newProj);
        // very approximate calculation of projection extent
        var extent = Extent.applyTransform(
            newProj.worldExtent_, fromLonLat);
        newProj.setExtent(extent);
        var newView = new View({
            projection: newProj,
            center: [0, 0],
            zoom: 2,
            minZoom: 2
        });
        MapObject.setView(newView);

        //缩放到图层范围
        newView.fit(FeatureExtent);
    }


}